package com.zc.framework.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by zc on 2016/10/19.
 */
public class NubmerUtils {

    public static final String FORMAT_INT = "#,##0";

    public static String formatNumber(BigDecimal number, int minimumFraction)
    {
        if (minimumFraction <= 0) {
            throw new IllegalArgumentException("the minimumFraction should greater than 0.");
        }
        if (number == null) {
            return null;
        }
        int scale = number.scale();

        String pattern = FORMAT_INT;
        if (scale <= minimumFraction) {
            pattern = pattern + "." + StringUtils.repeat("0", minimumFraction);
        } else {
            pattern = pattern + "." + StringUtils.repeat("0", minimumFraction) + StringUtils.repeat("#", scale - minimumFraction);
        }
        DecimalFormat format = new DecimalFormat(pattern);

        return format.format(number);
    }

}
